package net.diaowen.dwsurvey.dao.impl;

import net.diaowen.common.dao.BaseDaoImpl;
import net.diaowen.dwsurvey.dao.TorgDao;
import net.diaowen.dwsurvey.entity.Torg;
import org.aspectj.weaver.patterns.IToken;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import javax.jms.Session;
import javax.management.Query;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Repository
public class TorgDaoImpl extends BaseDaoImpl<Torg, String> implements TorgDao {
    @Override
    public List<Torg> selectall() {


        String sql="select * from t_org order by FIELD(id,1,10000)";
        //按id升序查找
        List<Object[]> list = this.getSession().createSQLQuery(sql).list();

//        Iterator iterator=list.iterator();
//        while(iterator.hasNext()){
//            System.out.println("输出一下t_org表中的数据");
//            System.out.println(iterator.next());
//        }
        List<Torg> list1=new ArrayList<Torg>();
        for (Object[] torg : list) {

            Torg temp=new Torg(torg[0].toString(),torg[1].toString(),torg[2].toString());
            list1.add(temp);
        }

        return list1;
    }

    @Override
    public int setorgname(int id,String orgname) throws Exception {
        Integer result;
        String sql="insert into t_org(id,org_id,org_name)value(?,?,?)";
        SQLQuery query = this.getSession().createSQLQuery(sql);
        query.setParameter(0,id);
        query.setParameter(1,id);
        query.setParameter(2,orgname);
        try{
            result=query.executeUpdate();
        }
        catch (Exception e){
            System.out.println("插入失败");
            result=0;
        }
        return result;
    }
}
